/*
 * $Id: CacheAspect.java 26 2012-05-11 10:20:13Z forwardly@gmail.com $
 * 
 * Copyright (c) 2001-2008 Accentrix Company Limited. All Rights Reserved.
 * 
 * Accentrix Company Limited. ("Accentrix") retains copyright on all text, source
 * and binary code contained in this software and documentation. Accentrix grants
 * Licensee a limited license to use this software, provided that this copyright
 * notice and license appear on all copies of the software. The software source
 * code is provided for reference purposes only and may not be copied, modified 
 * or distributed.
 * 
 * THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS," WITHOUT ANY WARRANTY OF
 * ANY KIND UNLESS A SEPARATE WARRANTIES IS PURCHASED FROM ACCENTRIX AND REMAINS
 * VALID.  ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ACCENTRIX SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING THE
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * IN NO EVENT WILL ACCENTRIX BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE
 * USE OF OR INABILITY TO USE SOFTWARE, EVEN IF ACCENTRIX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */
package com.lightsaber.trade.as.common.aspect;

import java.util.concurrent.Callable;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.lightsaber.trade.core.common.util.OSCacheUtil;

/**
 * Use to cache all query on syst_params
 * 
 * @author jliu
 * 
 */
@Aspect
@Component
public class CacheAspect {

    private final static Logger log = LoggerFactory.getLogger(CacheAspect.class);

    public static final String cacheGroup = "ParamObject";

    @Around(" execution (* com.lightsaber.trade.core.service.sysparam.SysParameteManager.*())")
    public Object cacheParamService(final ProceedingJoinPoint call) throws Throwable {

        String key = call.getSignature().getName();
        log.debug("cache key: {}", key);

        Callable<Object> function = new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                log.debug(" class is " + call.getSignature().getClass() + "");
                log.debug(call.toLongString());
                log.debug("{}", call.getTarget().getClass());

                Object result = null;
                try {
                    result = call.proceed();
                } catch (Throwable e) {
                    log.error(e.getMessage(), e);
                }
                return result;
            }
        };

        int refreshPeriod = -1; // never refresh

        return OSCacheUtil.cache(key, function, cacheGroup, refreshPeriod);
    }
}
